<template> <!-- 使用.sync 实现父子组件双向绑定 -->
  <el-dialog
    :title="'审核'"
    :visible.sync="visible"
  width="600px"
  @close="handleClose"
  >
  <el-form :model="auditData" label-width="80px">
    <el-form-item label="审核状态">
      <el-select v-model="auditData.auditStatus" placeholder="请选择审核状态" @change="handleChange">
        <el-option :value="1" label="审核通过"></el-option>
        <el-option :value="2" label="审核未通过"></el-option>
      </el-select>
    </el-form-item>

    <!-- 审核未通过时显示原因 -->
    <el-form-item v-if="auditData.auditStatus === 2" label="未通过原因" required>
      <el-input
        type="textarea"
        v-model="auditData.rejectionReason"
        placeholder="请输入未通过原因"
        rows="4"
      />
    </el-form-item>
  </el-form>

  <div slot="footer" class="dialog-footer">
    <el-button @click="handleClose">取消</el-button>
    <el-button type="primary" @click="submitAudit">提交</el-button>
  </div>
  </el-dialog>
</template>


<script>
import {doCheck} from "@/api/auditdialog/audit";
import qs from 'qs'
export default {
  name: 'AuditDialog',
  props: {
    // 父组件传递的数据
    visible: {type: Boolean, required: true},
    id_name: {type: String, required: true},
    table_name: {type: String, required: true},
  },
  data() {
    return {
      auditData: {
        idName: '',
        tableName: '',
        selectedIDs: null,
        auditStatus: '请选择审核状态',
        rejectionReason: ''
      }
    }
  },

  methods: {
    // 关闭对话框
    handleClose() {
      this.$emit('update:visible', false); // 关闭对话框
    },
    handleChange(event) {
      if (event === 1)
        this.auditData.rejectionReason = ''
    },
    // 提交审核数据
    submitAudit() {

      // 如果没有选择审核状态，则提示用户
      if (this.auditData.auditStatus === null || this.auditData.auditStatus === '请选择审核状态') {
        this.$message.warning('请选择审核状态');
        return;
      }
      // 如果没有选择审核状态，则提示用户
      if (this.auditData.auditStatus === 2 && this.auditData.rejectionReason === '') {
        this.$message.warning('请输入审核不通过原因');
        return;
      }
      this.auditData.tableName = this.table_name
      this.auditData.idName = this.id_name
      doCheck(this.auditData).then((response) => {
        this.$message.success("审核成功");
        this.visible = false;
        this.auditData = {
          idName: '',
          tableName: '',
          selectedIDs: null,
          auditStatus: '请选择审核状态',
          rejectionReason: ''
        }
        this.$emit('submit');
      });
    } ,
    setData(ids) {
      console.log(this.auditData)
      this.auditData.selectedIDs = ids;
    }
  },

};
</script>

<style scoped>
.dialog-footer {
  text-align: center;
}
</style>
